
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router';
import { Session } from '@/utils/storage';
import { Key } from '@/stores/auth';

export const dynamicRoutes: RouteRecordRaw[] = [
    {
        path: "/",
        name: "layout", // layout 的 children 数组为用户菜单页面的集合
        redirect: "/home",
        component: () => import('@/layout/index.vue'),
        children: [
            {
                path: "/home",
                name: "home",
                meta: {
                    hideInMenu: false, // 该菜单是否隐藏；true 为隐藏
                    title: "首页",
                    cache: true, // 为true，则缓存菜单
                    icon: "&#xe611;", // 图标
                },
                component: () => import("@/layout/main.vue"),
            },
            {
                path: "/grzx",
                name: "grzx",
                meta: {
                    title: "个人信息",
                    icon: "&#xe7fc;", // 图标
                },
                redirect:'/grzx',
                children: [
                    {
                        path: "/grzx",
                        name: "grzx",
                        meta: {
                            title: "个人信息",
                            icon: "&#xe90f;", // 图标
                        },
                        component: () => import("@/views/grzx/index.vue"),
                    },

                ],
            },

             
                                                {
                        path: "/news",
                        name: "news",
                        meta: {
                            title: "系统公告",
                            icon: "&#xe7fc;", // 图标
                        },
                        redirect:'/news',
                        children: [
                            {
                                path: "/news",
                                name: "news",
                                meta: {
                                    title: "系统公告",
                                    icon: "&#xe90f;", // 图标
                                },
                                component: () => import("@/views/news/index.vue"),
                            },
                            {
                                path: "/newsdetail/:id",
                                name: "newsdetail",
                                meta: {
                                    title: "系统公告详情",
                                    icon: "&#xe90f;", // 图标
                                },
                                component: () => import("@/views/news/detail.vue"),
                            },

                        ],
                    },

                
                            
                    
                        {
                            path: "/hetongguanli",
                            name: "hetongguanli",
                            meta: {
                                title: "合同管理",
                                icon: "&#xe7fc;", // 图标
                            },
                            redirect:'/hetongguanli',
                            children: [
                                {
                                    path: "/hetongguanli",
                                    name: "hetongguanli",
                                    meta: {
                                        title: "合同管理",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/hetongguanli/index.vue"),
                                },
                                {
                                    path: "/hetongguanlidetail/:id",
                                    name: "hetongguanlidetail",
                                    meta: {
                                        title: "合同管理",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/hetongguanli/detail.vue"),
                                },
                                {
                                    path: "/hetongguanlilist",
                                    name: "hetongguanlilist",
                                    meta: {
                                        title: "合同管理",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/hetongguanli/list.vue"),
                                },

                            ],
                        },
                                    
                            
                    
                        {
                            path: "/weibaojihua",
                            name: "weibaojihua",
                            meta: {
                                title: "维保计划",
                                icon: "&#xe7fc;", // 图标
                            },
                            redirect:'/weibaojihua',
                            children: [
                                {
                                    path: "/weibaojihua",
                                    name: "weibaojihua",
                                    meta: {
                                        title: "维保计划",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/weibaojihua/index.vue"),
                                },
                                {
                                    path: "/weibaojihuadetail/:id",
                                    name: "weibaojihuadetail",
                                    meta: {
                                        title: "维保计划",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/weibaojihua/detail.vue"),
                                },
                                {
                                    path: "/weibaojihualist",
                                    name: "weibaojihualist",
                                    meta: {
                                        title: "维保计划",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/weibaojihua/list.vue"),
                                },

                            ],
                        },
                                    
                            
                    
                        {
                            path: "/yonghu",
                            name: "yonghu",
                            meta: {
                                title: "用户",
                                icon: "&#xe7fc;", // 图标
                            },
                            redirect:'/yonghu',
                            children: [
                                {
                                    path: "/yonghu",
                                    name: "yonghu",
                                    meta: {
                                        title: "用户",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/yonghu/index.vue"),
                                },
                                {
                                    path: "/yonghudetail/:id",
                                    name: "yonghudetail",
                                    meta: {
                                        title: "用户",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/yonghu/detail.vue"),
                                },
                                {
                                    path: "/yonghulist",
                                    name: "yonghulist",
                                    meta: {
                                        title: "用户",
                                        icon: "&#xe90f;", // 图标
                                    },
                                    component: () => import("@/views/yonghu/list.vue"),
                                },

                            ],
                        },
                                    
                        {
                path: "/config",
                name: "config",
                meta: {
                    title: "系统管理",
                    icon: "&#xe7fc;", // 图标
                },
                redirect:'/config',
                children: [
                    {
                        path: "/config",
                        name: "config",
                        meta: {
                            title: "轮播图列表",
                            icon: "&#xe90f;", // 图标
                        },
                        component: () => import("@/views/config/index.vue"),
                    },

                ],
            },



            {
                path: '/401',
                name: 'NoPermission',
                component: () => import('@/views/error/401.vue'),
                meta: {
                    title: '401页面',
                    icon: 'ele-Warning',
                    cache: true,
                    hidden: false,
                }
            },
            {
                path: '/:path(.*)*', //  404匹配不存在的路由
                name: 'NotFound',
                component: () => import('@/views/error/404.vue'),
                meta: {
                    title: '未找到此页面',
                    cache: true,
                    hidden: true,
                }
            },

        ],
    },
    {
        path:'/login',
        name:'Login',
        component:()=>import('@/views/login/index.vue')
    }

]

const router = createRouter({
    // 参数获取的是 vite.config.ts 中base 属性值
    history: createWebHashHistory(),
    routes: dynamicRoutes,
});


router.beforeEach((to,from)=>{
    // const token = localStorage.getItem('token');
    //用户未登录，且跳转的目标路由不是登陆页面：则跳转登录页面/login


    // const token=Session.get(Key.accessTokenKey);
    // console.log(token);
    // if(!token && (to.path !=='/login')){
    //     //通过return返回一个跳转的路由对象，重定向到指定路由
    //     //  return router.push({path:'/login'});
    //     //  return {path:'/login'}


    //     return {path:'/login'}


    // }
    //已登录,正常进入目标路由
    return true;


})

// 路由后置守卫

export default router
